Abstract
The main premise of the mathematical financial analysis has always been trying to correctly estimate or predict the future behaviour of the price of any asset in order to get the best deal possible when trading it. As such, this work describes the employment of two well known economic models that delve into the work of theoretical asset pricing: The Asset Pricing Theory and the Implicit Measure of the Effective Bid-Ask Spread in Efficent Market</font> , also known as the Roll Model. This two models will be compared to real-world trading data in order to measure their effectivness.
Fair Value refers to the price agreed upon to by a willing buyer and a seller (Chen, 2021) .This value, under certain market conditions, can be mathematically modeled, and thus, estimated or forecasted.
Obviating the challenged posed by the not-ideal conditions of the market (agents are not neutral to risk, or not everyone possesses all the information necessary to consider the market as an efficent market), the forecasting of financial prices is not as straightforward as coming with an equation that can describe with 100% accuracy all factors intervening in its value, so the best way to model it, is through a stochastic process. In probability theory, a stochastic process is a mathematical object usually defined as a family of random variables (Dobb, 1990). Based on their properties, stochastic processes can be grouped into various categories, relevant to this work are: random walks and martingales.
The so-called Asset Pricing Theory Model utilizes a special case of a stochastic process, a martingale, to model the forecasting of the price of an asset. The basic premise that can define a martingale is a sequence of random variables for which, at a particular time, the expectation for the next value in the sequence is equal to the present value, regardless of all previous values. $$P_t = E[P_{t+1}] $$ (Muñoz-Elguezabal, 2017)
A simple implicit measue of the effective Bid-Ask spread in an Efficent Market, is the name given by Richard Roll to the model that aims to measure the effective bid-ask spread in the orderbook of a given market. This model is given by the equation: $$Spread = 2 \sqrt{-cov} $$ Where the covariance is given by the first-order serial autocovariance of the price changes. $$cov(\Delta p_t,\Delta p_{t-j})$$ (ROLL, 1984)
As such, the following models will be analyzed in their effectiveness in modeling the market by comparing their theoretical results to real-world trading data of the settled price of exchange of Bitcoin / USD.
The following work is subdivided into the following sections:
In order to run this notebook, it is necessary to have installed and/or have the requirements.txt file with the following:
The following are the file dependencies that are needed to run this notebook:
%%capture
#!{sys.executable} -m pip install -r requirements.txt
# Install all the pip packages in the requirements.txt
import numpy as np
import pandas as pd
import data as dt
import functions
import visualizations
import plotly.io as pio
# DataFrame Head
# Obtaining JSON file using the openOB method from the data Library
data_ob = dt.openOB()
# Orderbook timestamps
ob_ts = list(data_ob.keys())
# Timestamp listings
l_ts = [pd.to_datetime(i_ts) for i_ts in ob_ts]
# Metrics from Functions library
ob_df,_,_ = functions.OB_metrics(data_ob)
# midpricess from metrics dataframe
midprices = ob_df["Mid Price"]
pd.DataFrame(midprices.head())
| Mid Price | |
|---|---|
| 2021-07-05 13:06:46.571000+00:00 | 28272.5 |
| 2021-07-05 13:06:47.918000+00:00 | 28272.5 |
| 2021-07-05 13:06:49.414000+00:00 | 28272.5 |
| 2021-07-05 13:06:51.077000+00:00 | 28276.5 |
| 2021-07-05 13:06:52.426000+00:00 | 28276.5 |
# Midprices Histogram
pd.DataFrame(midprices).hist(bins="auto");
# Mid prices statistic description
midprices.describe()
count 2401.000000 mean 28351.878592 std 42.215634 min 28270.000000 25% 28315.500000 50% 28349.500000 75% 28384.500000 max 28444.500000 Name: Mid Price, dtype: float64
As stated in the introduction, this model consists on finding whether the asset price is a martingale process. In order to find if it is consistent with such model, we will provide the results on the martingale process evaluation of the afformentioned data and display it in a DataFrame.
The results will also be evaluated in three experiments:
# Martingale results for the entire data:
# Metrics from Functions library
ob_ts = list(data_ob.keys())
# Timestamp listings
l_ts = [pd.to_datetime(i_ts) for i_ts in ob_ts]
# Metrics from Functions library
ob_df,_,_ = functions.OB_metrics(data_ob)
# midpricess from metrics dataframe
midprices = ob_df["Mid Price"]
# Asset pricing model: Best estimator for future prices is current price
# Is it valid most of the time?
Model1_Exp_1 = functions.Model1_Exp_1(midprices)
Model1_Exp_1
| Amount | Ratio | |
|---|---|---|
| e1 | 1763.0 | 0.734277 |
| e2 | 637.0 | 0.265306 |
| total | 2400.0 | 2400.000000 |
As stated, now the data will be segmented into the trades that ocurred in each particular minute along the entire span of the data.
# Second Experiment (Every Minute Data)
Model1_Exp_2_1,Model1_Exp_2_2 = functions.Model1_Exp_2(midprices)
Model1_Exp_2_1.head()
| e1 | e2 | Total | Ratio e1 | Ratio e2 | |
|---|---|---|---|---|---|
| 13:6 | 6 | 2 | 8 | 0.750000 | 0.250000 |
| 13:7 | 27 | 13 | 40 | 0.675000 | 0.325000 |
| 13:8 | 31 | 8 | 39 | 0.794872 | 0.205128 |
| 13:9 | 27 | 11 | 38 | 0.710526 | 0.289474 |
| 13:10 | 30 | 10 | 40 | 0.750000 | 0.250000 |
Model1_Exp_2_2
| e1 Ratio Mean | e2 Ratio Mean | Total Trades | |
|---|---|---|---|
| 0 | 0.743515 | 0.256485 | 2340 |
For this experiment, now the martingale process will be evaluated with the weighted mid prices, both for the entire data and the minute segmented data.
# Experiment 3: Martingale Process with Weighted MidPrice
Model1_Exp_3_1,Model1_Exp_3_2,Model1_Exp_3_3 = functions.Model1_Exp_3(data_ob);
Model1_Exp_3_1
| Amount | Ratio | |
|---|---|---|
| e1 | 1622.0 | 0.675552 |
| e2 | 778.0 | 0.324032 |
| total | 2400.0 | 2400.000000 |
Model1_Exp_3_2.head()
| e1 | e2 | Total | Ratio e1 | Ratio e2 | |
|---|---|---|---|---|---|
| 13:6 | 6 | 2 | 8 | 0.750000 | 0.250000 |
| 13:7 | 27 | 13 | 40 | 0.675000 | 0.325000 |
| 13:8 | 26 | 13 | 39 | 0.666667 | 0.333333 |
| 13:9 | 26 | 12 | 38 | 0.684211 | 0.315789 |
| 13:10 | 27 | 13 | 40 | 0.675000 | 0.325000 |
Model1_Exp_3_3
| e1 Ratio Mean | e2 Ratio Mean | Total Trades | |
|---|---|---|---|
| 0 | 0.686378 | 0.313622 | 2340 |
As we've seen, this model states that the spread of the orderbook can be estimated using the 1st shit autocovariance of the price changes. For this model, the price considered is the Mid Price, and the results will be compared with the actual spread observed in the orderbook.
Model2_1,Model2_2 = functions.Model2(pd.DataFrame(midprices),ob_df)
Model2_1.head()
| Model Spread | Spread | Mid Price | Bid | Ask | Model Bid | Model Ask | |
|---|---|---|---|---|---|---|---|
| 2021-07-05 13:06:46.571000+00:00 | 0.070044 | 5.0 | 28272.5 | 28267.5 | 28277.5 | 28272.464978 | 28272.535022 |
| 2021-07-05 13:06:47.918000+00:00 | 0.070044 | 5.0 | 28272.5 | 28267.5 | 28277.5 | 28272.464978 | 28272.535022 |
| 2021-07-05 13:06:49.414000+00:00 | 0.070044 | 5.0 | 28272.5 | 28267.5 | 28277.5 | 28272.464978 | 28272.535022 |
| 2021-07-05 13:06:51.077000+00:00 | 0.070044 | 3.0 | 28276.5 | 28273.5 | 28279.5 | 28276.464978 | 28276.535022 |
| 2021-07-05 13:06:52.426000+00:00 | 0.070044 | 3.0 | 28276.5 | 28273.5 | 28279.5 | 28276.464978 | 28276.535022 |
Model2_2
| Spread Mean | Spread Variance | Calculated Spread | Spread Mean Difference | |
|---|---|---|---|---|
| 0 | 3.946272 | 6.021695 | 0.070044 | 3.876229 |
Model 1 or APT, was a success in correctly estimating most of the time the future price of the trade. Although this model is not suited for more than 1 step in the future estimations, it might be useful for trading systems.
Model 2 or Roll Model was unsuccesful in correctly estimating the spread of the Orderbook. This might be due to the size of the price of the asset.
We might be able to further observe the results of the first model comparing the midprices with its prices shifted one step into the future, to observe how the tendency of the price is estimated using the APT model.
g1 = visualizations.Model1_Stack_Bar_Graph(Model1_Exp_2_1,
["e1","e2"],
["Martingale Succesful Forecast", "Martingale Unsuccesful Forecast"],
"APT Model Results")
g1.show(renderer = "notebook")
As we can observe, most of the time the price is the same, proving to uss that APT is an adequate model.
Resuming the results of Experiment 1:
Resuming the results of Experiment 2:
Resuming the results of Experiment 3:
g2 = visualizations.Model1_Stack_Bar_Graph(Model1_Exp_3_2,
["e1","e2"],
["Martingale Succesful Forecast", "Martingale Unsuccesful Forecast"],
"APT, Volume-Weighted Mid Prices")
g2.show(renderer = "notebook")
It is a strange proposition attempting to generalize the value of a Random Variable with a constant, but, with this model it has been attempted, and as such, the constant thats trying to represent the random variable doesn't approximate to the Expected Value of the observed spread.
g3 = visualizations.Model2_price_timeseries(
Model2_1,
["Bid","Mid Price","Ask"],
["Observed Bid","Mid Price","Observed Ask"],
"Observed Spread"
)
g4 = visualizations.Model2_price_timeseries(
Model2_1,
["Model Bid","Mid Price","Model Ask"],
["Theoretical Bid","Mid Price","Theoretical Ask"],
"Theoretical Spread"
)
g3.show(renderer = "notebook")
g4.show(renderer = "notebook")
In the second graph, one can observe that the Spread distribution, mostly concentrates around the monetary difference of 2 between bid and Ask. It can be noted that this is at least twice the value of the proposed Spread encountered.
James Chen, Thomas Brock, Pete Rathburn. (2021). Fair Value. 21/06/2022, de Investopedia Sitio web: https://www.investopedia.com/terms/f/fairvalue.asp
Joseph L. Doob (1990). Stochastic processes. Wiley. pp. 46, 47.
Juan Francisco Muñoz-Elguezabal. (July 15, 2017). Asset Pricing Theory
ROLL, R. (1984). A Simple Implicit Measure of the Effective Bid-Ask Spread in an Efficient Market. The Journal of Finance, 39(4), 1127–1139. https://doi.org/10.1111/j.1540-6261.1984.tb03897.x
[1] Munnoz, 2020. Python project template. https://github.com/iffranciscome/python-project. (2021).